#import "Buffer.h"
#import "WalkScrollView.h"
#define ZOOM_STEP 1.5
@interface WalkScrollView (UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end

@implementation WalkScrollView 
@synthesize map;
-(id) initWithFrame:(CGRect)frame buffer:(Buffer*)buffer{
	[super initWithFrame:frame];
	
//	map = [[WalkMap alloc] initWithProperties:buffer.stageName];
    [map setDelegate:self];
	[self setContentSize:map.frame.size];
	[self addSubview:map];
	
	// calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [self frame].size.width  / [map frame].size.width;
    [self setMinimumZoomScale:minimumScale];
    [self setZoomScale:minimumScale];

	return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return map;
}
#pragma mark TapWalkMap methods

- (void)tapWalkMap:(WalkMap *)view gotSingleTapAtPoint:(CGPoint)tapPoint {
}

- (void)tapWalkMap:(WalkMap *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in
    float newScale = [self zoomScale] * ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [self zoomToRect:zoomRect animated:YES];
}

- (void)tapWalkMap:(WalkMap *)view gotTwoFingerTapAtPoint:(CGPoint)tapPoint {
    // two-finger tap zooms out
    float newScale = [self zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [self zoomToRect:zoomRect animated:YES];
}

#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [self frame].size.height / scale;
    zoomRect.size.width  = [self frame].size.width  / scale;
    
    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}
@end
//  public WalkPanel(WalkMap cur_map, int x, int y) {
//  
//    //X and Y are left in so that a initial camerapoint can be picked!
//    this.cur_map = cur_map;
//    
//    this.setBackground(new Color((float) 0.0, (float) 0.0, (float) 0.0));
//
//    //Camerawork code.
//    if(x<180) { Buffer.setCX(0); }
//    else if(x<cur_map.getWidth()-180) {
//      Buffer.setCX(cur_map.getWidth()-360);
//    }
//    else {
//      Buffer.setCX(x-180);  
//    }    
//    if(y<120) { Buffer.setCY(0); }
//    else if(y<cur_map.getHeight()-120) {
//      Buffer.setCY(cur_map.getHeight()-240);
//    }
//    else {
//      Buffer.setCY(y-120);  
//    }     
//    
//      //Don't forget to set the entity itself ;).
//    Buffer.Player().x=x;
//    Buffer.Player().y=y;
//    }
//    
// -(void) Update:(int)x y:(int)y {
//    this.cur_map = Buffer.getCurMap(); //necessary?
//    
//    //Camerawork code.
//    if(x<180) { Buffer.setCX(0); }
//    else if(x<(cur_map.getWidth()*24)-180) {
//      Buffer.setCX(x-180);
//    }
//    else {
//      Buffer.setCX((cur_map.getWidth()*24)-360);  
//    }    
//    if(y<120) { Buffer.setCY(0); }
//    else if(y<(cur_map.getHeight()*24)-120) {
//      Buffer.setCY(y-120);
//    }
//    else {
//      Buffer.setCY((cur_map.getHeight()*24)-240); 
//    }     
//    
//    //Minor detail: Don't forget to move the entity itself ;).
//    Buffer.Player().x=x;
//    Buffer.Player().y=y;
//    }
//    
// 
//-(void) paintComponent(Graphics g) {
//
//      super.paintComponent(g);
//
//      
//      g.setFont(Buffer.normaltxt);
//      
//      if(Buffer.ScaleRatio != 1.0) {
//        ((Graphics2D) g).scale(Buffer.ScaleRatio,Buffer.ScaleRatio);
//      }  
//      Lib_Image.renderMap(cur_map,g,this,Buffer.getCX(),Buffer.getCY(),360,240,0, 1);
//      Lib_Image.renderMap(cur_map,g,this,Buffer.getCX(),Buffer.getCY(),360,240,1, 1);
//      
//         
// 
//      
//      /* TODO: Move this to Lib_Image */
//      for(int i = 0; i < Buffer.numEnts(); i++) {
//        /* TODO: Be sure to add clipping of off screen entities !!! */
//        g.drawImage(Buffer.getEnt(i).getSprite().getFrame(Buffer.getEnt(i).f),Buffer.getEnt(i).x-Buffer.getCX(),Buffer.getEnt(i).y-Buffer.getCY(),this);
//      }
//      
//      Lib_Image.renderMap(cur_map,g,this,Buffer.getCX(),Buffer.getCY(),360,240,2,1);
//      
//      /* Layer 4 :) */
//      Lib_Image.renderMap(cur_map,g,this,Buffer.getCX(),Buffer.getCY(),360,240,6,1);
//      
//      /* Visible Obstruction Layer (for debug purposes) */
//      //Lib_Image.renderMap(cur_map,g,this,Buffer.getCX(),Buffer.getCY(),360,240,3,1);
//
//
//      //else {
//      //  Lib_Image.statsPage(g);
//      //}
//      if(WalkFrame.Mode() == 1) {
//         String temp=(Buffer.getEnt(Buffer.DetectTalk()).resptext);
//         if(temp.equals("shop") == true)
//         {
//          //this.cur_map = cur_map;
//          Shop.showshop(g,"start"); //current map parsed with it to get the shop.
//
//         }
//         else if(temp.equals("priest") == true)
//         {
//          Priest.showpriest(g);
//         }
//         else
//         {
//          //Lib_Image.textBox(g,Buffer.getEnt(Buffer.DetectTalk()).resptext);
//          Lib_Image.textBox(g,WalkFrame.TempResp());
//         }
//      }
//  }
//}  
